<template>
  <main-comp></main-comp>
  <div id="subLine-controller">
    <div class="item-wrapper">
      <div
        :class="['item', selectedLineIndex === index ? 'item-change' : '']"
        v-for="(item, index) in subLineData"
        :key="item.id"
        @click.stop="handleItemClick(item, index)"
      >
        <div
          class="box"
          :style="{ borderColor: item.color, backgroundColor: item.color }"
        ></div>
        <span :style="{ color: '#34c5cf' }">{{ item.name.slice(-3) }}</span>
      </div>
    </div>
  </div>
  <div class="active-panel">
    <div class="header">
      <strong>重点活动</strong>
      <span>{{ date }}</span>
    </div>
    <div class="content">
      <div class="active-wrapper" v-if="activityData.length">
        <div
          v-for="(item, index) in activityData"
          :key="item.id"
          :class="[
            'active-item',
            selectedActivityIndex === item.id ? 'item-active' : '',
          ]"
          @click="clickActiveItem(item, item.id)"
        >
          <div>{{ item.activedTime }}</div>
          <div>{{ item.activedPlace }}</div>
          <div>{{ item.activity }}</div>
        </div>
      </div>
      <a-empty v-else :image="simpleImage" description="无数据" />
    </div>
  </div>
</template>

<script setup>
import MainComp from '../components/SumComp.vue'
// import * as Cesium from 'cesium'
import { ref, onMounted, watch, onBeforeUnmount } from 'vue'
import { activity } from '@/store/staticData.js'
import { useLineData } from '@/store/index.js'
// import { renderLines } from '@/cesiumTools/effectController'
import {
  flyToLine,
  binkLineByName,
  addGradientCone,
  removeAllCones,
  flyToCone,
} from '@/cesiumTools/course.js'

let viewer
const currentStations = ref([])
const subLineData = ref([])
const activityData = ref([])
const lineDataStore = useLineData()
const isActiveLine = ref(false)
const isActive = ref(false)
const selectedLineIndex = ref(null)
const selectedActivityIndex = ref(null)

// 定义当前日期
const date = new Date().toLocaleDateString()

// 点击线路显示对应线路及事件
const handleItemClick = (item, index) => {
  // 检查是否再次点击同一线路
  if (selectedLineIndex.value === index) {
    isActiveLine.value = !isActiveLine.value
  } else {
    isActiveLine.value = true
    selectedLineIndex.value = index // 更新当前选中线路索引
  }
  if (isActiveLine.value) {
    const { paths, name, stationsList, color } = item
    // const position = []
    // paths.forEach((path) => {
    //   position.push(path.lng, path.lat)
    // })
    // console.log(position)
    // const options = {
    //   positions: position,
    //   color: Cesium.Color.fromCssColorString(color),
    //   name: name,
    // }
    // renderLines(viewer, options)
    flyToLine(viewer, name)
    binkLineByName(name)
    currentStations.value = stationsList
    activityData.value = activity
    showActiveArea(color)
  } else {
    removeAllCones(viewer)
  }
}

const showActiveArea = (color) => {
  removeAllCones(viewer)
  const ids = activityData.value.map((item) => item.id)
  // 这里坐标直接取index和id对应的站点坐标
  const activedStations = currentStations.value
    .map((item, index) => {
      item.id = index
      return item
    })
    .filter((item) => ids.includes(item.id))
  activedStations.forEach((item) => {
    const { position, id } = item
    // 添加渐变圆柱
    addGradientCone(viewer, {
      position,
      color,
      name: id,
    })
  })
}

// 点击活动显示对应事件
const clickActiveItem = (item, index) => {
  if (selectedActivityIndex.value === index) {
    isActive.value = !isActive.value
  } else if (isActiveLine.value) {
    isActive.value = true
    selectedActivityIndex.value = index // 更新当前选中活动索引
  }
  console.log('活动点击', isActive.value)
  if (isActive.value) {
    const { activedTime, activedPlace, activity } = item
    console.log(activedTime, activedPlace, activity)
    flyToCone(viewer, index)
  }
}

// 监听切换线路时，活动点击样式清除
watch(selectedLineIndex, (newVal, oldVal) => {
  if (newVal !== oldVal) {
    selectedActivityIndex.value = null
    isActive.value = false
  }
})

onMounted(() => {
  viewer = lineDataStore.viewer
  console.log(viewer)
  subLineData.value = lineDataStore.lineData
  console.log(subLineData.value)
  activityData.value = activity
  console.log(activityData.value)
})
//跳转时清除所有圆柱
onBeforeUnmount(() => {
  removeAllCones(viewer)
})
</script>

<style scoped>
#subLine-controller {
  position: absolute;
  bottom: 8%;
  left: 33.5%;
  transform: translateX(-50%);
  width: 190px;
  height: 180px;
  background-color: rgba(0, 0, 0, 0.3);
  border: 1px solid #664a16;
  padding: 4px;
  display: flex;
  flex-direction: column;
}

.box {
  width: 15px;
  height: 15px;
  border-width: 1px;
  border-style: solid;
  background: transparent;
  user-select: all;
  cursor: pointer;
}

.item {
  width: 75px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 3px;
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid #885f12;
  color: #37b3bb;
  font-size: 14px;
  pointer-events: all;
  cursor: pointer;
}

.item:hover {
  color: #fff;
  border: 1px solid #d8961a;
}

.item-change {
  color: #fff;
  border: 1px solid #d8961a;
}

.item > span {
  line-height: 9.005px;
}

.item > input {
  outline: none;
  border: none;
  transition: all 0.3s ease;
}

.item-wrapper {
  display: flex;
  padding: 0 5px;
  justify-content: space-between;
  align-content: space-around;
  flex-wrap: wrap;
  flex: 1;
}

.active-panel {
  width: 590px;
  height: 180px;
  border: 1px solid #ab7818;
  background-color: rgba(0, 0, 0, 0.3);
  position: absolute;
  left: 38.8%;
  bottom: 8%;
  color: #fff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.active-panel > .header {
  height: 35px;
  line-height: 35px;
  padding: 5px;
  background: rgb(255, 255, 255);
  background-image: linear-gradient(
    180deg,
    rgba(255, 255, 255, 1) 9%,
    rgba(211, 156, 50, 1) 57%
  );
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: -0.521vw;
  font-family: '等线Bold';
}

.active-panel > .header > strong {
  font-weight: normal;
  margin-right: 10px;
}

.active-panel > .header > span {
  font-size: 16px;
}

.content {
  flex: 1;
}

.active-wrapper {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100%;
}

.active-item {
  width: 150px;
  height: 100px;
  border: 1px solid #ab7818;
  background-color: rgba(214, 174, 41, 0.1);
  margin: 4px;
  text-align: center;
  pointer-events: all;
  cursor: pointer;
  transition: all 0.3s linear;
  color: #37b3bb;
  padding: 20px;
  font-size: 16px;
}

.active-item:hover {
  background-color: rgba(214, 174, 41, 0.3);
  color: #fff;
}

.item-active {
  background-color: rgba(214, 174, 41, 0.3);
  color: #fff;
}
</style>
